package cn.dglydrpy.study.common.routingDataSource;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class RoutingDataSourceAutoConfiguration {

	Logger logger = LoggerFactory.getLogger(RoutingDataSourceAutoConfiguration.class);

	/*  mysql数据库 start */
	/**
	 * mysql数据库
	 * @return
	 */
    @Bean
    @ConfigurationProperties(prefix = "mysql5.datasource")
    public DataSourceProperties mysqlDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    public DataSource mysqlDataSource(){
        DataSourceProperties dataSourceProperties = mysqlDataSourceProperties();
        log.info("mysql: "+dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager mysqlTxManager(DataSource mysqlDataSource){
        return new DataSourceTransactionManager(mysqlDataSource);
    }
	/*  mysql数据库 end */

	/*  oracle数据库  start */
	/**
	 * oracle数据库
	 * @return
	 */
    @Bean
    @ConfigurationProperties(prefix ="oracle10.datasource")
    public DataSourceProperties oracleDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    public DataSource oracleDataSource(){
        DataSourceProperties dataSourceProperties = oracleDataSourceProperties();
        log.info("oracle: "+dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager oracleTxManager(DataSource oracleDataSource){
        return new DataSourceTransactionManager(oracleDataSource);
    }
	/*  oracle数据库  end */

	/**
	 * 	mysql dataSource
	 */
//	@Bean
//	@ConfigurationProperties(prefix = "mysql5.datasource")
//	public DataSource mysqlDataSource(){
//		logger.info("create mysql dataSource...");
//		return DataSourceBuilder.create().build();
//	}

	/**
	 * 	oracle dataSource
	 */
//	@Bean
//	@ConfigurationProperties(prefix = "oracle10.datasource")
//	public DataSource oracleDataSource(){
//		logger.info("create oracle dataSource...");
//		return DataSourceBuilder.create().build();
//	}

	@Bean
	@Primary
	public DataSource primaryDataSource(
			@Autowired @Qualifier("mysqlDataSource") DataSource mysqlDataSource,
			@Autowired @Qualifier("oracleDataSource") DataSource oracleDataSource
	){

		RoutingDataSource routingDataSource = new RoutingDataSource();
		Map<Object, Object> map = new HashMap<>();
		map.put("mysql",mysqlDataSource);
		map.put("oracle",oracleDataSource);

		routingDataSource.setTargetDataSources(map);

		return routingDataSource;

	}



}
